% AESTEST
% AES test script.
% AES test in accordance with
% Morris Dworkin, Recommendation for Block Cipher Modes of Operation
% Methods and Techniques
% NIST Special Publication 800-38A, 2001 Edition
% Appendix F: Example Vectors for Modes of Operation of the AES

% Stepan Matejka, 2011, matejka[at]feld.cvut.cz
% $Revision: 1.1.0 $  $Date: 2011/10/12 $

% plain text = 64 bytes / four 128-bits blocks
% 6bc1bee22e409f96e93d7e117393172a
% ae2d8a571e03ac9c9eb76fac45af8e51
% 30c81c46a35ce411e5fbc1191a0a52ef
% f69f2445df4f9b17ad2b417be66c3710

pth = {'6b' 'c1' 'be' 'e2' '2e' '40' '9f' '96'...
    'e9' '3d' '7e' '11' '73' '93' '17' '2a'...
    'ae' '2d' '8a' '57' '1e' '03' 'ac' '9c'...
    '9e' 'b7' '6f' 'ac' '45' 'af' '8e' '51'...
    '30' 'c8' '1c' '46' 'a3' '5c' 'e4' '11'...
    'e5' 'fb' 'c1' '19' '1a' '0a' '52' 'ef'...
    'f6' '9f' '24' '45' 'df' '4f' '9b' '17'...
    'ad' '2b' '41' '7b' 'e6' '6c' '37' '10'};
pt = hex2dec(pth);

keyh = {'2b' '7e' '15' '16' '28' 'ae' 'd2' 'a6'...
    'ab' 'f7' '15' '88' '09' 'cf' '4f' '3c'};
key = hex2dec(keyh);

s = aesinit(key);

% ------------------------------------------------------------------------
% ECB test of AES-128

ct = aes(s, 'enc', 'ecb', pt);
pt2 = aes(s, 'dec', 'ecb', ct);

ctcheckh = {'3a' 'd7' '7b' 'b4' '0d' '7a' '36' '60'...
    'a8' '9e' 'ca' 'f3' '24' '66' 'ef' '97'...
    'f5' 'd3' 'd5' '85' '03' 'b9' '69' '9d'...
    'e7' '85' '89' '5a' '96' 'fd' 'ba' 'af'...
    '43' 'b1' 'cd' '7f' '59' '8e' 'ce' '23'...
    '88' '1b' '00' 'e3' 'ed' '03' '06' '88'...
    '7b' '0c' '78' '5e' '27' 'e8' 'ad' '3f'...
    '82' '23' '20' '71' '04' '72' '5d' 'd4'};
ctcheck = hex2dec(ctcheckh);

% test ciphertext & decrypted plaintext
fprintf('ECB - Ciphertext errors: %d, plaintext errors: %d.\n',...
    sum(ctcheck(:) ~= ct(:)),...
    sum(pt(:) ~= pt2(:)));

% ------------------------------------------------------------------------
% CBC test of AES-128

ivh = {'00' '01' '02' '03' '04' '05' '06' '07'...
    '08' '09' '0a' '0b' '0c' '0d' '0e' '0f'};
iv = hex2dec(ivh);

ct = aes(s, 'enc', 'cbc', pt, iv);
pt2 = aes(s, 'dec', 'cbc', ct, iv);

ctcheckh = {'76' '49' 'ab' 'ac' '81' '19' 'b2' '46'...
    'ce' 'e9' '8e' '9b' '12' 'e9' '19' '7d'...
    '50' '86' 'cb' '9b' '50' '72' '19' 'ee'...
    '95' 'db' '11' '3a' '91' '76' '78' 'b2'...
    '73' 'be' 'd6' 'b8' 'e3' 'c1' '74' '3b'...
    '71' '16' 'e6' '9e' '22' '22' '95' '16'...
    '3f' 'f1' 'ca' 'a1' '68' '1f' 'ac' '09'...
    '12' '0e' 'ca' '30' '75' '86' 'e1' 'a7'};
ctcheck = hex2dec(ctcheckh);

% test ciphertext & decrypted plaintext
fprintf('CBC - Ciphertext errors: %d, plaintext errors: %d.\n',...
    sum(ctcheck(:) ~= ct(:)),...
    sum(pt(:) ~= pt2(:)));

% ------------------------------------------------------------------------
% OFB test of AES-128

ct = aes(s, 'enc', 'ofb', pt, iv);
pt2 = aes(s, 'dec', 'ofb', ct, iv);

ctcheckh = {'3b' '3f' 'd9' '2e' 'b7' '2d' 'ad' '20'...
    '33' '34' '49' 'f8' 'e8' '3c' 'fb' '4a'...
    '77' '89' '50' '8d' '16' '91' '8f' '03'...
    'f5' '3c' '52' 'da' 'c5' '4e' 'd8' '25'...
    '97' '40' '05' '1e' '9c' '5f' 'ec' 'f6'...
    '43' '44' 'f7' 'a8' '22' '60' 'ed' 'cc'...
    '30' '4c' '65' '28' 'f6' '59' 'c7' '78'...
    '66' 'a5' '10' 'd9' 'c1' 'd6' 'ae' '5e'};
ctcheck = hex2dec(ctcheckh);

% test ciphertext & decrypted plaintext
fprintf('OFB - Ciphertext errors: %d, plaintext errors: %d.\n',...
    sum(ctcheck(:) ~= ct(:)),...
    sum(pt(:) ~= pt2(:)));

% ------------------------------------------------------------------------
%  CTR test of AES-128

ct = aes(s, 'enc', 'ctr', pt);
pt2 = aes(s, 'dec', 'ctr', ct);

ctcheckh = {'87' '4d' '61' '91' 'b6' '20' 'e3' '26'...
    '1b' 'ef' '68' '64' '99' '0d' 'b6' 'ce'...
    '98' '06' 'f6' '6b' '79' '70' 'fd' 'ff'...
    '86' '17' '18' '7b' 'b9' 'ff' 'fd' 'ff'...
    '5a' 'e4' 'df' '3e' 'db' 'd5' 'd3' '5e'...
    '5b' '4f' '09' '02' '0d' 'b0' '3e' 'ab'...
    '1e' '03' '1d' 'da' '2f' 'be' '03' 'd1'...
    '79' '21' '70' 'a0' 'f3' '00' '9c' 'ee'};
ctcheck = hex2dec(ctcheckh);

% test ciphertext & decrypted plaintext
fprintf('CTR - Ciphertext errors: %d, plaintext errors: %d.\n',...
    sum(ctcheck(:) ~= ct(:)),...
    sum(pt(:) ~= pt2(:)));

% ------------------------------------------------------------------------
%  CFB1 test of AES-128

ct = aes(s, 'enc', 'cfb', pt, iv, 1);
pt2 = aes(s, 'dec', 'cfb', ct, iv, 1);

% test ciphertext & decrypted plaintext
fprintf('CFB1 - Plaintext errors: %d.\n',...
    sum(pt(:) ~= pt2(:)));

% ------------------------------------------------------------------------
%  CFB8 test of AES-128

ct = aes(s, 'enc', 'cfb', pt, iv, 8);
pt2 = aes(s, 'dec', 'cfb', ct, iv, 8);

% test ciphertext & decrypted plaintext
fprintf('CFB8 - Plaintext errors: %d.\n',...
    sum(pt(:) ~= pt2(:)));

% ------------------------------------------------------------------------
%  CFB128 test of AES-128

ct = aes(s, 'enc', 'cfb', pt, iv, 128);
pt2 = aes(s, 'dec', 'cfb', ct, iv, 128);

% test ciphertext & decrypted plaintext
fprintf('CFB128 - Plaintext errors: %d.\n',...
    sum(pt(:) ~= pt2(:)));

% ------------------------------------------------------------------------
%  CFB128 test of AES-256

keyh = {'60' '3d' 'eb' '10' '15' 'ca' '71' 'be'...
    '2b' '73' 'ae' 'f0' '85' '7d' '77' '81'...
    '1f' '35' '2c' '07' '3b' '61' '08' 'd7'...
    '2d' '98' '10' 'a3' '09' '14' 'df' 'f4'};
key = hex2dec(keyh);

s = aesinit(key);

ct = aes(s, 'enc', 'cfb', pt, iv, 128);
pt2 = aes(s, 'dec', 'cfb', ct, iv, 128);

% test ciphertext & decrypted plaintext
fprintf('CFB128 - Plaintext errors: %d.\n',...
    sum(pt(:) ~= pt2(:)));

% ------------------------------------------------------------------------
% end of file
